package com.example.spring.restful.controller;

import com.example.spring.restful.model.Article;
import com.example.spring.restful.model.ResponseResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author : zhayh
 * @date : 2021-2-27 21:15
 * @description : 自定义统一返回码数据结构
 */

@Slf4j
@RestController
@RequestMapping("/custom")
public class ArticleCustomController {
    @GetMapping("/articles/{id}")
    public ResponseResult getArticle(@PathVariable("id") Long id) {
        Article article = Article.builder()
                .id(id)
                .author("niit")
                .content("Sprint Boot Restful 接口")
                .createTime(new Date())
                .title("Spring Boot入门")
                .build();
        log.info("article:" + article);
        return ResponseResult.success(article);
    }

    @PostMapping("/articles")
    public ResponseResult saveArticle(@RequestBody Article article) {
        log.info("saveArticle: " + article);
        return ResponseResult.success("保存成功");
    }

    @PutMapping("/articles")
    public ResponseResult updateArticle(@RequestBody Article article) {
        if (article.getId() == null) {
            // article.id是必传参数，因为通常根据id去修改数据
            // TODO 抛出一个自定义的异常
        }
        log.info("updateArticle: " + article);
        return ResponseResult.success("更新成功");
    }

    @DeleteMapping("/articles/{id}")
    public ResponseResult deleteArticle(@PathVariable("id") Long id) {
        log.info("deleteArticle: " + id);
        return ResponseResult.success("删除成功");
    }

//    @PostMapping("/articles")
//    public ResponseResult saveArticle(@RequestParam String author,
//                                    @RequestParam String title,
//                                    @RequestParam String content,
//                                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
//                                    @RequestParam Date createTime) {
//
//        log.info("saveArticle:" + createTime);
//        return ResponseResult.success();
//    }
}
